package com.mdz.sbgl.Controller;
import com.mdz.sbgl.Entity.Urlset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

/**
 * @author 孟德志
 * @create 2019-12-15-2:46
 */
@Controller
public class LoginController {

    @Value("${geturl}")
    private String geturl;

    @Value("${autologin}")
    private String autologin;

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpSession session;

    @RequestMapping("/login")
    public String login(){
        session.setAttribute("autologin", autologin);
        return "login";
    }

    @RequestMapping("/inx")
    public String inx(){
        return "inx";
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/index_gly")
    public String indexgly(){
        session.setAttribute("username", "超级管理员");
        session.setAttribute("loginname", "administrator");
        return "index_gly";
    }

    @RequestMapping("/setup/index")
    public String setindex(){
        return "setup/index";
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin()
    {
        return "skin";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main()
    {
        return "main";
    }

    @GetMapping("/gssz")
    public String gssz()
    {
        return "gssz";
    }

    @GetMapping("/desk")
    public String desk()
    {
        return "desk";
    }
    @GetMapping("/zdyyj")
    public String zdyyj()
    {
        return "zdyyj";
    }

    @GetMapping("/yjian")
    public String yjian()
    {
        return "yjian";
    }

    @GetMapping("/sbfl")
    public String sbfl()
    {
        return "sbfl";
    }

    @GetMapping("/saoma")
    public String saoma()
    {
        return "saoma";
    }

    @GetMapping("/tis")
    public String tis()
    {
        return "tis";
    }

    @ResponseBody
    @GetMapping("/is_dat")
    public Boolean is_dat(){
        File file=new File("setdone.dat");
        if (file.exists()){
            return true;
        }else {
            return false;
        }
    }

    @ResponseBody
    @GetMapping(value = "/sitemap", produces = {"application/xml;charset=UTF-8"})
    public Urlset getsitemapXml(){
        try {
            File file=new File("D:\\http.xml");
            FileWriter fw=new FileWriter(file);
            fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
            fw.write("<Document xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"终端接口XML Schema-3.0.xsd\" SN=\"323232323\" Version=\"3.0\" License=\"00120-100290-002\">\n");
            fw.write("\t<Events>\n");
            fw.write("\t\t<Event Name=\"SalesWareHouseOut\" MainAction=\"WareHouseOut\">\n");
            fw.write("\t\t\t<ActionMapping>\n");
            fw.write("\t\t\t\t<Action Name=\"WareHouseOut\">\n");
            fw.write("\t\t\t\t\t<ActionData>CorpOrderID</ActionData>\n");
            fw.write("\t\t\t\t\t<ActionData>Actor</ActionData>\n");
            fw.write("\t\t\t\t\t<ActionData>ActDate</ActionData>\n");
            fw.write("\t\t\t\t\t<ActionData>ToCorpID</ActionData>\n");
            fw.write("\t\t\t\t\t<ActionData>Code</ActionData>\n");
            fw.write("\t\t\t\t</Action>\n");
            fw.write("\t\t\t</ActionMapping>\n");
            fw.write("\t\t\t<DataMaping>\n");
            fw.write("\t\t\t\t<MetaData Name=\"CorpOrderID\" Type=\"String\"/>\n");
            fw.write("\t\t\t\t<MetaData Name=\"Actor\" Type=\"String\"/>\n");
            fw.write("\t\t\t\t<MetaData Name=\"ActDate\" Type=\"Date\"/>\n");
            fw.write("\t\t\t\t<MetaData Name=\"ToCorpID\" Type=\"String\"/>\n");
            fw.write("\t\t\t\t<MetaData Name=\"Code\" Type=\"String\"/>\n");
            fw.write("\t\t\t</DataMaping>\n");
            fw.write("\t\t\t<DataField>\n");
            fw.write("\t\t\t\t<Data Code=\"88310450002283173232\" CorpOrderID=\"2011601606\" Actor=\"王强\" ActDate=\"2020-11-16 9:46:09\" ToCorpID=\"000000273697\"/>\n");
            fw.write("\t\t\t</DataField>\n");
            fw.write("\t\t</Event>\n");
            fw.write("\t</Events>\n");
            fw.write("</Document>\n");
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Urlset urlset=new Urlset();
        Urlset.Url url=new Urlset.Url();
        url.setLoc("张三");
        url.setLastmod("李四");
        url.setChangefreq("王五");
        url.setPriority("赵六");
        urlset.getUrl().add(url);
        return urlset;
    }
//    @GetMapping("/xwj")
//    public String xwj()
//    {
////        try {
////            File file=new File("aa.xml");
////            FileWriter fileWriter=new FileWriter(file,false);
////            fileWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
////            fileWriter.write("<Document xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"终端接口XML Schema-3.0.xsd\" SN=\"323232323\" Version=\"3.0\" License=\"00120-100290-002\">");
////            fileWriter.close();
////        } catch (IOException e) {
////            e.printStackTrace();
////        }
//        ClassLoaderTemplateResolver resolver=new ClassLoaderTemplateResolver();
//        //模板所在目录，相对于当前classloader的classpath。
//        resolver.setPrefix("thymeleaf/");
//        //模板文件后缀
//        resolver.setSuffix(".html");
//        TemplateEngine templateEngine = new TemplateEngine();
//        templateEngine.setTemplateResolver(resolver);
//
//        //构造上下文(Model)
//        try {
//            StringBuilder fileName = new StringBuilder();
//            FileWriter write = new FileWriter(fileName.toString());
//            write.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
//            write.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "xwj";
//    }

    @GetMapping("/bmry")
    public String bmry()
    {
        return "bmry";
    }

    @GetMapping("/csh")
    public String csh()
    {
        return "deletedb";
    }

    @GetMapping("/whfl")
    public String whfl()
    {
        return "whfl";
    }

    @GetMapping("/wenhb")
    public String wenhb()
    {
        return "wenhb";
    }

    @GetMapping("/whjldy")
    public String whjldy()
    {
        return "whjldy";
    }

    @GetMapping("/gly")
    public String gly()
    {
        return "gly";
    }

    @PostMapping("/sbgl/sbxxb")
    public void   testDownload(HttpServletResponse response , Model model) {

    }
    @PostMapping("/")
    public void   Download(HttpServletResponse response , Model model) {
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginname");
        session.removeAttribute("gsid");
        session.removeAttribute("username");
        session.removeAttribute("userid");
        return "redirect:/login";
    }

    @RequestMapping("/sbgl/{sbbh}")
    public String out(HttpSession session, @PathVariable("sbbh") String sbbh){
        session.removeAttribute("loginname");
        session.removeAttribute("gsid");
        session.removeAttribute("username");
        session.removeAttribute("userid");
        session.setAttribute("username", "");
        return "redirect:/sbgl/sbxxb/sbwh/"+sbbh;
    }

    @RequestMapping("/resetPwd")
    public String resetPwd(){
        return "resetPwd";
    }
    @RequestMapping("/tianjia")
    public String tianjia(){
        return "tianjia";
    }
    @RequestMapping("/weihu")
    public String weihu(){
        return "weihu";
    }
    @RequestMapping("/xiugai")
    public String xiugai(){
        return "xiugai";
    }
    @RequestMapping("/shouye")
    public String shouye(){
        return "shouye";
    }
    @RequestMapping("/help")
    public String help(){
        return "help";
    }

    @RequestMapping("/delwhjl")
    public String delwhjl(){
        return "delwhjl";
    }

    @ResponseBody
    @RequestMapping("/geturl")
    public String geturl(){
//        String ip = "http://pv.sohu.com/cityjson?ie=utf-8";
//        String inputLine = "";
//        String read = "";
//        try {
//            URL url = new URL(ip);
//            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
//            while ((read = in.readLine()) != null) {
//                inputLine += read;
//            }
//            return inputLine.split(",")[0].split(":")[1].replace("\"", "");
//        } catch (Exception e) {
////            e.printStackTrace();
//            //获取内网IP地址
////            try {
////                String IPA= Inet4Address.getLocalHost().getHostAddress();
////                return IPA;
////            }catch (Exception e1){
////                return "请核对是否有网卡";
////            }
//            return geturl;
//        }
        return geturl;
    }

    @GetMapping("/fwdz")
    public String fwdz()
    {
        return "fwdz";
    }

}
